import json
import boto3

from framework.common.pt_logger import logger
from util.athenaUtil import QueryExecutor
from util.eventManager import EventManager

athena = boto3.client('athena')
events = boto3.client('events')


def start_initiator(metadata=None, payload=None):
    assert "s3_path" in payload and "athena_sql" in payload
    # batch_id = metadata["time"].split("T")[0]

    athena_sql = payload["athena_sql"]  # JimmyMo: athena CATS query result, casts type to JSON
    s3_path = payload["s3_path"]  # JimmyMo: path folder to store the query result
    athenaQuery = QueryExecutor()
    athenaQuery.cats_query(athena_sql, s3_path)
    # JimmyMo: selective_coreProducts_initiator为pseudo spider initiator，所有的数据在athena中都存在了，不需要有worker爬取
    # 上面的athenaQuery.cats_query会将athena中的选取的数据转存至指定的s3_path，后续发送spider完成的event，触发后续的分析任务即可
    EventManager().pt_spider_state_changed(state="COMPLETED",
                                           metadata={
                                               "SpiderName": "SelectiveSpider",
                                               "InitiatorName": "CoreProducts"
                                           },
                                           payload=payload)


if __name__ == "__main__":
    metadata = {}
    payload = {
        "athena_sql": 'SELECT "asin", "rawNodeId", "rank", "LowestPrice" FROM "coreproducts"."manual_testingttt_2021_07_01" where rawnodeid=\'12897221\'',
        "s3_path": "s3://pt-data-dev/staging/best-sellers/flatten_2021-07-02-manual/"
    }
    start_initiator(metadata=metadata, payload=payload)
